package easy.找出字符串中第一个匹配项的下标;

public class Solution {
    public int strStr(String haystack, String needle) {
        int n = haystack.length();
        int m = needle.length();
        for (int i = 0; i <= n - m; i++) {
            //每次与子串匹配都设一个flag
            boolean flag = true;
            for (int j = 0; j < m; j++) {
                if (haystack.charAt(i + j) != needle.charAt(j)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                return i;
            }
        }
        return -1;
//        return haystack.indexOf(needle);
    }

    public static void main(String[] args) {
        String haystack = "leetcode";
        String needle = "leeto";
        String haystack2 = "sadbutsad";
        String needle2 = "sad";
        Solution solution = new Solution();
        System.out.println(solution.strStr(haystack, needle));
        System.out.println(solution.strStr(haystack2, needle2));
    }
}
